//
//  HNSH_ChatOpenRedDetailViewController.m
//  NongYe
//
//  Created by 李宇廷 on 2023/2/6.
//

#import "HNSH_ChatOpenRedDetailViewController.h"
#import "HNSH_ChatOpenRedDetailCell.h"
#import "HNSH_ChatOpenRedDetailHeader.h"
#import "HNSH_ChatReceiveRedViewController.h"

@interface HNSH_ChatOpenRedDetailViewController ()<UITableViewDelegate,UITableViewDataSource>


/* dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;

/* rightItemButton */
@property (nonatomic, strong) UIButton *rightItemButton;

/* headerView */
@property (nonatomic, strong) HNSH_ChatOpenRedDetailHeader *headerView;

/* detailInfo */
@property (nonatomic, strong) HNSH_ChatRedDetailInfoModel *detailInfo;

@end

static NSString *HNSH_ChatOpenRedDetailCellID = @"HNSH_ChatOpenRedDetailCellID";

@implementation HNSH_ChatOpenRedDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 128)];
    imgV.image = [UIImage imageNamed:@"chat_open_red_detail_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
    
    [self showNavTitle:@"红包详情" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    [self.view addSubview:self.rightItemButton];
    
    
    self.tableView.rowHeight = 64;
    [self.tableView registerClass:[HNSH_ChatOpenRedDetailCell class] forCellReuseIdentifier:HNSH_ChatOpenRedDetailCellID];
    self.tableView.tableHeaderView = self.headerView;
    
    [self loadRedDetail];
}


#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.detailInfo.open_list.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_ChatOpenRedDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_ChatOpenRedDetailCellID];
    cell.model = self.detailInfo.open_list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NYLSHelpCenterModel *model = self.dataArray[indexPath.row];
//    NSString *title = model.name;
//    NSString *content = model.answer;
//    CGFloat height = [content heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth-72];
//    NYLSHelpCenterAlert *alert = [[NYLSHelpCenterAlert alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 128+height+kSafeAreaBottomHeight)];
//    alert.titleString = title;
//    alert.contentString = content;
//    [alert showView];
}

#pragma mark lazy
- (UIButton *)rightItemButton {
    if (!_rightItemButton) {
        _rightItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightItemButton.frame = CGRectMake(kScreenWidth-88, kStatusBarHeight, 88, 44);
        [_rightItemButton setTitle:@"红包记录" forState:UIControlStateNormal];
        [_rightItemButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _rightItemButton.titleLabel.font = [UIFont systemFontOfSize:14];
        __weak __typeof(&*self)weakSelf = self;
        [[_rightItemButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_ChatReceiveRedViewController *vc = [[HNSH_ChatReceiveRedViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _rightItemButton;
}

- (HNSH_ChatOpenRedDetailHeader *)headerView {
    if (!_headerView) {
        _headerView = [[HNSH_ChatOpenRedDetailHeader alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 232)];
        
    }
    return _headerView;
}

#pragma mark 红包详情
- (void)loadRedDetail {
    [HNSH_ChatNetServer requestRongCloudRedPacketDetailWithRedId:self.redId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.detailInfo = [HNSH_ChatRedDetailInfoModel yy_modelWithDictionary:responseObject.resultset];
            self.headerView.detailInfo = self.detailInfo;
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
